﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Qplus.beans;
using Qplus.Commands;

namespace Qplus.UI
{
    public partial class PersonalSetting : PhoneApplicationPage
    {
        //public string name;
        //public int sex;
        //public string mail;
        //public string birthday;
        //public int canStrangerChat;
        //public string level;
        //public int headType;
        //public string headValue;
        //public MightList[] MightList;
        //public string Current_max_friend_count;
        //public string Max_friend_count_upper;
        //public string Task_score;
        //public appList[] appList;
        //UserInfo userinfo;

        private static bool isFirstNavigatedTo = true;
        public PersonalSetting()
        {
            InitializeComponent();
            PTB.AddHandler(ButtonEx.MouseLeftButtonDownEvent, new MouseButtonEventHandler(PTB_MouseLeftButtonDown), true);
            PTB.AddHandler(ButtonEx.MouseLeftButtonUpEvent, new MouseButtonEventHandler(PTB_MouseLeftButtonUp), true);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (isFirstNavigatedTo)
            {
                try
                {
                    #region
                    //name = (string)QUser.Instance.personalInfo["name"];
                    //sex = (int)QUser.Instance.personalInfo["sex"];
                    //mail = (string)QUser.Instance.personalInfo["mail"];
                    //birthday = (string)QUser.Instance.personalInfo["birthday"];
                    //canStrangerChat = (int)QUser.Instance.personalInfo["canStrangerChat"];
                    //level = (string)QUser.Instance.personalInfo["level"];
                    //headType = (int)QUser.Instance.personalInfo["headType"];
                    //headValue = (string)QUser.Instance.personalInfo["headValue"];
                    //MightList = null;//QUser.Instance.personalInfo["MightList;
                    //Current_max_friend_count = (string)QUser.Instance.personalInfo["Current_max_friend_count"];
                    //Max_friend_count_upper = (string)QUser.Instance.personalInfo["Max_friend_count_upper"];
                    //Task_score = (string)QUser.Instance.personalInfo["Task_score"];
                    //appList = null;//QUser.Instance.personalInfo["appList"];
                    #endregion
                    if (0 == (int)QUser.Instance.personalInfo["sex"]) maleRbtn.IsChecked = true;
                    else if (1 == (int)QUser.Instance.personalInfo["sex"]) femaleRbtn.IsChecked = true;
                    else if (2 == (int)QUser.Instance.personalInfo["sex"]) unknowRbtn.IsChecked = true;
                    birthdayDP.Value = Convert.ToDateTime((string)QUser.Instance.personalInfo["birthday"]);
                    namePTB.Text = (string)QUser.Instance.personalInfo["name"];
                    mailPTB.Text = (string)QUser.Instance.personalInfo["mail"];
                    if (0 == (int)QUser.Instance.personalInfo["canStrangerChat"]) distrubChkBox.IsChecked = false;
                    else if (1 == (int)QUser.Instance.personalInfo["canStrangerChat"]) distrubChkBox.IsChecked = true;
                }
                catch (Exception ee)
                {
                    System.Diagnostics.Debug.WriteLine(ee.Message);
                }
                isFirstNavigatedTo = false;
            }
            else
            {

            }
            base.OnNavigatedTo(e);
        }
        #region HEAD
        private void camBtnEx_Click(object sender, RoutedEventArgs e)
        {
            CameraCaptureTask camCTask = new CameraCaptureTask();
            camCTask.Show();
            camCTask.Completed += new EventHandler<PhotoResult>(camCTask_Completed);
        }
        void camCTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bmpSource = new BitmapImage();
                bmpSource.SetSource(e.ChosenPhoto);
                //显示头像
                headImg.Source = bmpSource;
                //recRC = new RecordControl()
                //{
                //    MsgType = msgTypeEnum.receivePic,
                //    Pic = bmpSource,
                //};
                //senRC = new RecordControl()
                //{
                //    MsgType = msgTypeEnum.sendPic,
                //    Pic = bmpSource,
                //};
            }
            else
            {
                //do nothing
            }
        }
        #endregion
        #region SEX
        private void maleBtn_Click(object sender, RoutedEventArgs e)
        {
            if (maleRbtn.IsChecked == false)
            {
                maleRbtn.IsChecked = true;
            }
        }

        private void femaleBtn_Click(object sender, RoutedEventArgs e)
        {
            if (femaleRbtn.IsChecked == false)
            {
                femaleRbtn.IsChecked = true;
            }
        }

        private void unknowBtn_Click(object sender, RoutedEventArgs e)
        {
            if (unknowRbtn.IsChecked == false)
            {
                unknowRbtn.IsChecked = true;
            }
        }

        private void maleRbtn_Checked(object sender, RoutedEventArgs e)
        {
            QUser.Instance.modifyInfo.sex = 0;
            maleImgSelect.Visibility = System.Windows.Visibility.Visible;
            maleImgUnSelect.Visibility = System.Windows.Visibility.Collapsed;
            maleTxtBlk.Foreground = new SolidColorBrush(Colors.Blue);
        }

        private void maleRbtn_Unchecked(object sender, RoutedEventArgs e)
        {
            maleImgSelect.Visibility = System.Windows.Visibility.Collapsed;
            maleImgUnSelect.Visibility = System.Windows.Visibility.Visible;
            maleTxtBlk.Foreground = new SolidColorBrush(Colors.White);
        }

        private void femaleRbtn_Checked(object sender, RoutedEventArgs e)
        {
            QUser.Instance.modifyInfo.sex = 1;
            femaleImgSelect.Visibility = System.Windows.Visibility.Visible;
            femaleImgUnSelect.Visibility = System.Windows.Visibility.Collapsed;
            femaleTxtBlk.Foreground = new SolidColorBrush(Colors.Red);
        }

        private void femaleRbtn_Unchecked(object sender, RoutedEventArgs e)
        {
            femaleImgSelect.Visibility = System.Windows.Visibility.Collapsed;
            femaleImgUnSelect.Visibility = System.Windows.Visibility.Visible;
            femaleTxtBlk.Foreground = new SolidColorBrush(Colors.White);
        }

        private void unknowRbtn_Checked(object sender, RoutedEventArgs e)
        {
            QUser.Instance.modifyInfo.sex = 2;
            unknowImgSelect.Visibility = System.Windows.Visibility.Visible;
            unknowImgUnSelect.Visibility = System.Windows.Visibility.Collapsed;
            unknowTxtBlk.Foreground = new SolidColorBrush(Colors.Green);
        }

        private void unknowRbtn_Unchecked(object sender, RoutedEventArgs e)
        {
            unknowImgSelect.Visibility = System.Windows.Visibility.Collapsed;
            unknowImgUnSelect.Visibility = System.Windows.Visibility.Visible;
            unknowTxtBlk.Foreground = new SolidColorBrush(Colors.White);
        }
        #endregion
        #region BIRTHDAY
        private void birthdayDP_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            var dp = sender as DatePicker;
            QUser.Instance.modifyInfo.birthday = dp.Value.ToString();
        }
        private void birthdayTxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            birthdayTxtBox.Text = "";
        }
        private void birthdayBtnEx_Click(object sender, RoutedEventArgs e)
        {
            DatePicker datePick1 = new DatePicker();
            //datePick1.ValueChanged += datePicker_ValueChanged;
        }
        #endregion
        #region NAME
        private void namePTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            QUser.Instance.modifyInfo.name = namePTB.Text;
        }
        private void nicknameBtnEx_Click(object sender, RoutedEventArgs e)
        {
            birthdayTxtBox.Focus();
        }
        #endregion
        #region EMAIL
        private void mailPTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            QUser.Instance.modifyInfo.mail = mailPTB.Text;
        }
        private void emailTxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            emailTxtBox.Text = "";
        }

        private void emailBtnEx_Click(object sender, RoutedEventArgs e)
        {
            emailTxtBox.Focus();
        }
        #endregion
        #region DISTURB
        private void disturbBtnEx_Click(object sender, RoutedEventArgs e)
        {
            if (distrubChkBox.IsChecked == true)
            {
                distrubChkBox.IsChecked = false;
            }
            else if (distrubChkBox.IsChecked == false)
            {
                distrubChkBox.IsChecked = true;
            }
            else
            {
                //something wrong
            }
        }

        private void distrubChkBox_Checked(object sender, RoutedEventArgs e)
        {
            QUser.Instance.modifyInfo.canStrangerChat = 1;
            uncheckedImg.Visibility = System.Windows.Visibility.Collapsed;
            checkedImg.Visibility = System.Windows.Visibility.Visible;
        }

        private void distrubChkBox_Unchecked(object sender, RoutedEventArgs e)
        {
            QUser.Instance.modifyInfo.canStrangerChat = 0;
            checkedImg.Visibility = System.Windows.Visibility.Collapsed;
            uncheckedImg.Visibility = System.Windows.Visibility.Visible;
        }
        #endregion
        #region OK
        private void okBtnEx_Click(object sender, RoutedEventArgs e)
        {
            ModifyUserCommand command = new ModifyUserCommand(QUser.Instance.userID.ToString(), System.Text.Encoding.UTF8.GetString(QUser.Instance.key, 0, QUser.Instance.key.Length), QUser.Instance);
            command.set(QUser.Instance.modifyInfo.name,
                QUser.Instance.modifyInfo.mail, "",
                QUser.Instance.modifyInfo.birthday,
                QUser.Instance.modifyInfo.sex,
                QUser.Instance.modifyInfo.canStrangerChat,
                QUser.Instance.modifyInfo.headType,
                QUser.Instance.modifyInfo.headValue);//, null);
            command.execute(QUser.Instance.url);
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }
        #endregion
        #region TEST
        private void PTB_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            PTB.ActionIcon = new BitmapImage(new Uri("/Qplus;component/Pic/user_info_name_down.png", UriKind.RelativeOrAbsolute));
            PTB.Text = "";
        }
        private void PTB_MouseLeftButtonUp(object sender, RoutedEventArgs e)
        {
            PTB.ActionIcon = new BitmapImage(new Uri("/Qplus;component/Pic/user_info_name.png", UriKind.RelativeOrAbsolute));
        }



        /*private void datePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            DateTime date = (DateTime)e.NewDateTime;
            birthdayBtnEx.Content = date.ToString("d");
        }*/
        #endregion
    }
}